import config from '@/config';
import * as vConsole from 'vconsole'

const logCutSize = 50;
// {
//   if(config.debug) {
//     new vConsole();
//   }
// }

/**
 * 常用日志
 * @param msg
 * @param param
 */
export const log = (msg, ...param) => {
  if (config.debug) {
    const paramStr = param.length > 0 ? JSON.stringify(param) : '';
    const outParam = paramStr.length <= logCutSize ? paramStr : param;
    console.log(`${formatDate(new Date())} - ${msg}`, outParam);
  }
};

/**
 * 需要醒目的日志
 * @param msg
 * @param param
 */
export const error = (msg, ...param) => {
  console.error(`${formatDate(new Date())} - ${msg}`, param.length > 0 ? param : '');
};

/**
 * 日期格式化类
 * @param pattern
 * @param date
 * @return {*}
 */
const formatDate = (date, pattern = 'yyyy-MM-dd hh:mm:ss.S') => {
  let o = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds(),
    'q+': Math.floor((date.getMonth() + 3) / 3),
    'S': date.getMilliseconds(),
  };
  if (/(y+)/.test(pattern)) {
    pattern = pattern.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
  }
  for (let k in o) {
    if (new RegExp('(' + k + ')').test(pattern)) {
      pattern = pattern.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)));
    }
  }
  return pattern;
};
